package com.studyadmin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyadmin.config.QueryPageParam;
import com.studyadmin.dto.ArticleInsertBO;
import com.studyadmin.entity.PersonArticle;
import com.studyadmin.entity.Student;
import com.studyadmin.service.PersonArticleService;
import com.studyadmin.util.JsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 文章管理表 前端控制器
 * </p>
 *
 * @author lyl
 * @since 2023-03-27
 */
@RestController
@RequestMapping("/studyadmin/person-article")
@CrossOrigin
public class PersonArticleController {

    @Autowired
    PersonArticleService articleService;
    /**
     * 文章设置分类
     * @param categoryName 分类名
     * @return 结果
     */
    @GetMapping("/getCategory")
    public JsonUtil chapterContent(@RequestParam(value = "categoryName") String categoryName){
//        System.out.println("文章分类名"+categoryName);
        return articleService.getCategoriesByName(categoryName);
    }

    /**
     * 添加文章
     * @return
     */
    @PostMapping("/create")
//    @OperationLogSys(desc = "添加文章", operationType = OperationType.INSERT)
    public JsonUtil articleCreate(@RequestBody ArticleInsertBO bo) {
//        System.out.println(bo);

        return articleService.insertOrUpdateArticle(bo);
    }

    /**
     * 文章 分页展示
     * @return
     */
    @RequestMapping("/articleList")
    public JsonUtil stuList(@RequestBody QueryPageParam query){
        System.out.println(query);
        HashMap param = query.getParam();
        String author = (String) param.get("author");               //学生名称
        String title = (String) param.get("title"); //班级名称
        String categoryName = (String) param.get("categoryName"); //登录者教师学号
        String stuNum=(String) param.get("stuNum");
        Integer art_status= (Integer) param.get("artStatus");

        Page<PersonArticle> studentPage = new Page<>();
        studentPage.setCurrent(query.getPageNum());    //设置每页多少条记录
        studentPage.setSize(query.getPageSize());      //设置当前显示第几页
        QueryWrapper<PersonArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(" pa.art_status", art_status);

        if (StringUtils.isNotBlank(author) && !"null".equals(author)){
            queryWrapper.like(" pa.author", author);
        }
        if (StringUtils.isNotBlank(title) && !"null".equals(title)){
            queryWrapper.like(" pa.title", title);
        }
        if (StringUtils.isNotBlank(categoryName) && !"null".equals(categoryName)){
            queryWrapper.like(" pc.category_name", categoryName);
        }
        if (StringUtils.isNotBlank(stuNum) && !"null".equals(stuNum)){
            queryWrapper.like(" pa.user_id", stuNum);
        }
        IPage result = articleService.pageForArt(studentPage, queryWrapper); //最后进行分页查询

        return JsonUtil.suc(result.getRecords(), result.getTotal());
    }


    /**
     * 文章的详细情况
     * @param stuNum
     * @param articleId
     * @return
     */
    @GetMapping ("/articleDetails")
    public JsonUtil articleDetailsByStu(@RequestParam(value = "stuNum") String stuNum,
                                        @RequestParam(value = "articleId") String articleId){
        return articleService.articleDetailsByStuSer(stuNum,articleId);

    }




}

